package com.nand.module.user.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nand.module.user.R;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by 孝杰 on 2019/12/12.
 */
public class UserInfoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    int[] icons = {R.mipmap.icon_order, R.mipmap.icon_order, R.mipmap.icon_order, R.mipmap.icon_order, R.mipmap.icon_order, R.mipmap.icon_order, R.mipmap.icon_order};
    String[] titles = {"个人中心", "服务商信息", "常见问题", "企业信息", "关于","退出登录"};
    String[] content = {"", "鸿芯信息化团队", "1、常见问题1\\N2、常见问题2", "企业信息", "鸿芯信息化协同平台：1.0.0V",""};


    private final Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private setOnClickListener mSetOnClickListener;

    public UserInfoListAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_info_top_layout, parent, false);
            UserInfoListAdapter.TopViewHolder viewHolder = new UserInfoListAdapter.TopViewHolder(view);
            return viewHolder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_info_list_layout, parent, false);
            UserInfoListAdapter.ListViewHolder viewHolder = new UserInfoListAdapter.ListViewHolder(view);
            return viewHolder;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (position == 0) {
            TopViewHolder topViewHolder = (TopViewHolder) holder;
            topViewHolder.mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mSetOnClickListener.onItemClick(view, 0);
                }
            });
            topViewHolder.mLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mSetOnClickListener.onItemClick(view, 1);
                }
            });
        } else {
            final int i = position - 1;
            final ListViewHolder listViewHolder = (ListViewHolder) holder;
            listViewHolder.mImage.setImageResource(icons[i]);
            listViewHolder.mText.setText(titles[i]);
            listViewHolder.belowView.setText(content[i]);
            listViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (position == 1||position == 6) {
                        mOnItemClickListener.onItemClick(view, i);
                    } else {
                        int visibility = listViewHolder.belowView.getVisibility();
                        if (visibility == 0) {
                            listViewHolder.belowView.setVisibility(View.GONE);
                        } else {
                            listViewHolder.belowView.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return titles.length + 1;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnClickListener(setOnClickListener setOnClickListener) {
        mSetOnClickListener = setOnClickListener;
    }

    public interface setOnClickListener {
        void onItemClick(View view, int position);
    }

    public static class ListViewHolder extends RecyclerView.ViewHolder {

        private final ImageView mImage;
        private final TextView mText;
        private final View mView;
        private final TextView belowView;

        public ListViewHolder(View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.im_icod);
            mText = itemView.findViewById(R.id.tv_user_title);
            belowView = itemView.findViewById(R.id.item_view);
            mView = itemView;
        }
    }

    public static class TopViewHolder extends RecyclerView.ViewHolder {

        private final ImageView mBack;
        private final ImageView mLogin;

        public TopViewHolder(View itemView) {
            super(itemView);
            mBack = itemView.findViewById(R.id.im_back);
            mLogin = itemView.findViewById(R.id.im_user_login);

        }
    }


}
